import { RESPONSE_SUCCESS_CODE, RESPONSE_SUCCESS_MSG } from '@/constants/response.constant'
import { BusinessException } from '@/common/exceptions/biz.exception'
export class ResultData<T = any> {
	// @ApiProperty({ type: 'number', default: RESPONSE_SUCCESS_CODE })
	code: number

	// @ApiProperty({ type: 'string', default: RESPONSE_SUCCESS_MSG })
	msg: string

	// @ApiProperty({ type: 'object' })
	data?: T

	constructor(code = RESPONSE_SUCCESS_CODE, msg: string = RESPONSE_SUCCESS_MSG, data?: T) {
		this.code = code
		this.msg = msg
		this.data = data
	}

	static success<T>(data?: T, msg?: string) {
		return new ResultData(RESPONSE_SUCCESS_CODE, msg, data)
	}
	static fail(msg, code: number = 500) {
		if (msg.includes(':')) {
			;[code, msg] = msg.split(':')
		}
		return new ResultData(code, msg)
	}
	static error(msg) {
		return new BusinessException(msg)
	}
}
